using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Supertris
{
    class Piece
    {
        public enum PieceType
        {
            I,
            J,
            L,
            T,
            S,
            Z,
            O,
            NumTypes
        }

        public static readonly Point[][] PIECES = {
            new Point[] { new Point(0, -1), new Point(0, 0), new Point(0, 1), new Point(0, 2) },
            new Point[] { new Point(0, -1), new Point(0, 0), new Point(0, 1), new Point(-1, 1) },
            new Point[] { new Point(0, -1), new Point(0, 0), new Point(0, 1), new Point(1, 1) },
            new Point[] { new Point(0, -1), new Point(-1, 0), new Point(0, 0), new Point(1, 0) },
            new Point[] { new Point(0, -1), new Point(1, -1), new Point(-1, 0), new Point(0, 0) },
            new Point[] { new Point(-1, -1), new Point(0, -1), new Point(0, 0), new Point(1, 0) },
            new Point[] { new Point(0, -1), new Point(1, -1), new Point(0, 0), new Point(1, 0) }
        };

        public static readonly Color[] PIECE_COLORS = { Color.Blue, Color.Red, Color.Green, Color.Yellow, Color.Purple, Color.Orange, Color.Brown };

        public enum Rot
        {
            CW,
            CCW
        }

        public Block[] blocks;
        public Point[] blockPositions;

        public Piece(Block baseBlock, Point[] initBlockPositions)
        {
            blockPositions = new Point[initBlockPositions.Length];
            blocks = new Block[blockPositions.Length];

            for (int i = 0; i < blocks.Length; i++)
            {
                blockPositions[i] = initBlockPositions[i];
                blocks[i] = new Block(baseBlock);
            }
        }

        public Piece(Piece copy)
        {
            blockPositions = new Point[copy.blockPositions.Length];
            blocks = new Block[copy.blockPositions.Length];
            for (int i = 0; i < blocks.Length; i++)
            {
                blockPositions[i] = copy.blockPositions[i];
                blocks[i] = copy.blocks[i];
            }
        }

        public void Copy(Piece copy)
        {
            blockPositions = new Point[copy.blockPositions.Length];
            blocks = new Block[copy.blockPositions.Length];
            for (int i = 0; i < blocks.Length; i++)
            {
                blockPositions[i] = copy.blockPositions[i];
                blocks[i] = copy.blocks[i];
            }
        }

        public void Rotate(Rot rotation)
        {
            for (int i = 0; i < blockPositions.Length; i++)
            {
                int buffer;
                switch (rotation)
                {
                    case Rot.CW:
                        buffer = blockPositions[i].Y;
                        blockPositions[i].Y = blockPositions[i].X;
                        blockPositions[i].X = -buffer;
                        break;

                    case Rot.CCW:
                        buffer = blockPositions[i].X;
                        blockPositions[i].X = blockPositions[i].Y;
                        blockPositions[i].Y = -buffer;
                        break;
                }
            }
        }
    }
}
